import { scrollMixin } from "../../../utils/mixin"; //引入加载更多
import { Toast } from "vant";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      active: 0,
      url: "",
      projectList: [],
      title: "已预约项目",
      show_worker: false,
      show: false,
      keyword: "",
      popupList: {
        data: [],
        page: 1,
        total_page: 0
      },
      point:{},

      // more
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    
    if (this.$route.query.name === "store") {
      this.title = "门店预约";
      this.url = "plugin.appointment.frontend.appointment-service.get-list-by-store";
    } else {
      this.title = this.fun.getCustomTextLang("appointment.worker", "技师") + "预约";
      this.url = "plugin.appointment.frontend.appointment-service.get-list-by-worker";
    }
    this.active = 0;
    this.show = false;
    this.init();
    this.fun.getLocation()
    .then((res)=> {
      this.point = res.point;
      this.getData();
    })
    .catch((err)=> {
      console.log(err);
      this.getData();
    });
  },
  computed: {
    appointmentLang() {
      if (this.$store.state.shopLanguage.appointment) {
        return this.$store.state.shopLanguage.appointment;
      } else {
        return { worker: "技师", project: "项目", service: "服务" };
      }
    },
    addressSet(item){
      return function(item){
        let addr = item.service_address;
        if( item.service_address && item.service_type == 1){
          return `${addr.province}${addr.city}${addr.district}${addr.street || ''}${addr.address || ''}`
        }
      }
    }
  },
  methods: {
    init() {
      this.page = 1;
      this.isLoadMore = true;
      this.total_page = 0;
      this.projectList = [];
      this.show_worker = false;
    },
    goToAdress(service_address) {
      let point = {
        lng:Number(service_address.longitude),
        lat:Number(service_address.latitude)
      };
      this.fun.goToWXAdress(point, "", service_address.address);
    },
    async showWorkPopup(item) {
      this.show = true;
      this.keyword = "";
      this.popupList.service_id = item.id;
      this.popupList.page = 1;
      this.popupList.last_page = 0;
      await this.getTechnician();
    },
    onClickLeft() {
      this.show = false;
      this.keyword = "";
      this.popupList = {
        data: [],
        page: 1,
        total_page: 0
      }
    },
    async searchLis() {
      console.log(this.keyword)
      this.popupList.page = 1;
      this.popupList.total_page = 0;
      await this.getTechnician();
    },
    async getTechnician() {
      let { msg, result, data } = await $http.post("plugin.appointment.frontend.project-goods.get-list-by-store-id", { page: this.popupList.page, kwd: this.keyword }, ".");
      console.log(data);
      if (!result) return this.$toast(msg);
      this.popupList.last_page = data.last_page;
      this.popupList.data = data.data;
      console.log(this.popupList.data);
    },
    async tapMore(){
      if(this.popupList.page >= this.popupList.last_page) return;
      this.popupList.page = this.popupList.page + 1;
      let { msg, result, data } = await $http.post("plugin.appointment.frontend.project-goods.get-list-by-store-id", { page: this.popupList.page, kwd: this.keyword }, ".");
      if (!result) return this.$toast(msg);
      let nextData = data.data;
      this.popupList.data = this.popupList.data.concat(nextData);
    },
    async selectTechnician(item) {
      console.log(item.id, this.popupList);
      let { msg, result, data } = await $http.post("plugin.appointment.frontend.project-goods.change-worker", { worker_id: item.id, service_id: this.popupList.service_id }, ".");
      if (!result) return this.$toast(msg);
      console.log(data);
      this.onClickLeft();
      this.getData();
    },
    sure(id) {
      this.$dialog
        .confirm({ message: "您确定要取消此预约吗?" })
        .then(() => {
          this.cancelDate(id);
        })
        .catch(() => {
          console.log("cancel");
        });
    },
    cancelDate(id) {
      $http
        .get(
          "plugin.appointment.frontend.appointment-service.cancel-service",
          {
            id: id
          },
          "获取中"
        )
        .then(response => {
          if (response.result === 1) {
            Toast(response.msg);
            this.getData();
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    selectTab(e) {
      this.getData();
    },
    getData() {
      this.init();
      let json = {
        status: this.active,
        page: this.page
      }
      json.lng = this.point.lng;
      json.lat = this.point.lat;
      $http
        .get(
          this.url,
          json,
          "获取中"
        )
        .then(response => {
          this.fun.setWXTitle(this.title);
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.projectList = response.data.data;
            if(response.data?.set){
              this.show_worker = response.data.set.show_worker ? true : false;
            }
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http
          .get(
            this.url,
            {
              status: this.active,
              page: this.page
            },
            "加载中"
          )
          .then(
            response => {
              that.isLoadMore = true;
              if (response.result === 1) {
                var myData = response.data.data;
                that.projectList = that.projectList.concat(myData); //数组拼接
              } else {
                that.page = that.page - 1;
                that.isLoadMore = false;
              }
            },
            function(response) {
              // error callback
            }
          );
      }
    }
  },
  components: {}
};
